import os

import yaml

from common.CommonHeaders import CommonHeaders
from common.logger import logger
from core.request_handler import RequestHandler



class WxmpOperations:

    def __init__(self, config_file, wxmp_file):

        # 打印当前工作目录以帮助调试
        print(f"当前工作目录: {os.getcwd()}")

        # 生成绝对路径
        base_dir = os.path.dirname(os.path.abspath(__file__))  # 获取当前文件的绝对路径
        config_file_path = os.path.join(base_dir, '..', 'config', config_file)
        wxmp_file_path = os.path.join(base_dir, '..', 'wxmp', wxmp_file)

        # 正确解析路径
        config_file_path = os.path.abspath(config_file_path)
        wxmp_file_path = os.path.abspath(wxmp_file_path)

        print(f"配置文件路径: {config_file_path}")
        print(f"数据文件路径: {wxmp_file_path}")

        # 读取配置文件
        with open(config_file_path, 'r') as file:
            config = yaml.safe_load(file)
        self.base_url = config['base_url']  # 设置基础URL
        self.wxmp_file_path = wxmp_file_path  # 设置数据文件路径
        self.request_handler = RequestHandler(self.base_url)  # 初始化请求处理器
        # 记录base_url，便于后续跟踪或调试
        logger.info(f"base_url: {self.base_url}")
        self.token = None  # 初始化token为空

    # def login(self, mobile, code, grantType, terminalType):
    def wxmp(self, payload, common_headers):

        headers = self.common_headers.get_headers()
        # headers = {
        #     "Authorization": "OTU0dXF0MG5zamVoZ2l4dGEzN2luMDg1Ond6MW51enB4amQ1bG5xN3JhM2V3aHFzcHhuMnBnZHQy",
        #     "Applicationid": "464781535210464262",
        #     "Butenantid": "439938383512058064",
        #     "Accept": "application/json, text/plain, */*",
        #     "Versiontype": "2",
        #     "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
        #     "Content-Type": "application/json",
        #     "Host": "test-school-dl.workeredu.com",
        #     "Accept-Encoding": "gzip, deflate, br",
        #     "Connection": "keep-alive",
        #     "token": "eyJ0eXAiOiJKc29uV2ViVG9rZW4iLCJhbGciOiJIUzI1NiJ9.eyJVdWlkIjoiNDAwNjY0NzA0MzQwMTE5MjYyOjk1NHVxdDBuc2plaGdpeHRhMzdpbjA4NTpjY2QyZTE2YTdiMmE0NmJlOThiYzVmOWU1N2NmNzAxNiIsIlVzZXJJZCI6IjQwMDY2NDcwNDM0MDExOTI2MiIsIkVtcGxveWVlSWQiOiIwIiwiRW1wbG95ZWVOYW1lIjoiIiwiaWF0IjoxNzI0MzkzOTI2LCJuYmYiOjE3MjQzOTM5MjYsImV4cCI6MTcyNDQwMTEyNn0.vA33gviiwesX88MxwzucgT-SgzHGzisxwDeOUfXzJZc"
        # }
        logger.info("********开始登录过程**************")
        response = self.request_handler.post_request('api/wxmp/admin/notification/page', headers, json=payload)

        # 检查响应状态码
        if response.status_code == 200:
            response_json = response.json()
            if response_json["code"] == 0:
                # 提取 token
                # optimizeCountSql = response_json["data"].get("optimizeCountSql")
                return {
                    "code": response_json["code"],
                    "msg": response_json["msg"],
                    "data": response_json["data"]
                }
            else:
                return {"code": response_json["code"], "msg": response_json["msg"]}
        else:
            logger.error(f"请求失败，状态码: {response.status_code}, 错误信息: {response.text}")
            return {"code": response.status_code, "msg": response.text}
